//index.js
//获取应用实例
var httpClient = require('../../utils/httpClient.js');
var serverConstants = require('../../constants/serverConstants.js');

Page({
  data: {
    showTopTips: false,
    src: '',
    specrange: ['包', '个', '袋', '条', '箱', '瓶', '提'],
    specindex: 0,
    good: {
    },
    chooseCatogery: {}
  },
  specchange: function (e) {
    var good = this.data.good;
    good.spec = this.data.specrange[e.detail.value];
    this.setData({
      specindex: e.detail.value
    });
    this.setData({
      good: good,
    })
  },
  codeInput: function (e) {
    var good = this.data.good;
    good.code = e.detail.value;
    this.setData({
      good: good,
    })
  },
  nameInput: function (e) {
    var good = this.data.good;
    good.name = e.detail.value;
    this.setData({
      good: good,
    })
  },
  stockInput: function (e) {
    var good = this.data.good;
    good.stock = e.detail.value;
    this.setData({
      good: good,
    })
  },
  priceInput: function (e) {
    var good = this.data.good;
    good.price = e.detail.value;
    this.setData({
      good: good,
    })
  },
  bidInput: function (e) {
    var good = this.data.good;
    good.bid = e.detail.value;
    this.setData({
      good: good,
    })
  },
  descInput: function (e) {
    var good = this.data.good;
    good.description = e.detail.value;
    this.setData({
      good: good,
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  choosePic: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        });
        wx.uploadFile({
          url: serverConstants.serverHost + '/fileUpload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function (res) {
            wx.hideToast();
            var data = JSON.parse(res.data);
            that.setData({
              src: serverConstants.serverHost + data.result
            });
            var good = that.data.good;
            good.picUrl = data.result;
            that.setData({
              good: good,
            })
          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) {
              }
            });
          }
        })
      }
    });
  },
  //扫描商品
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        var good = this.data.good;
        that.queryGood(res.result);
      }
    });
  },
  queryGood: function (code) {
    var that = this;
    var shopId = wx.getStorageSync("shopId");
    httpClient.get('/good/view/' + shopId + '/' + code, function (good) {
      good.price = good.price;
      good.bid = good.bid;
      good.stock = 0;
      var src = serverConstants.serverHost + good.picUrl;
      that.setData({
        good: good,
        src: src
      });
    }, function (error) {
      if (error.code == '5000004') {
        var good = that.data.good;
        good.code = code;
        that.setData({
          good: good
        });
      }
    });

  },
  addGood: function () {
    var good = this.data.good;
    if (this.data.chooseCatogery.id) {
      good.category = this.data.chooseCatogery.id;
    }
    if (!good.spec) {
      good.spec = this.data.specrange[this.data.specindex];
      this.setData({
        good: good
      })
    }
    var that = this;
    var shopId = wx.getStorageSync("shopId");
    this.data.good.shopId = shopId;
    httpClient.post('/good/add', this.data.good, function (result) {
      wx.showToast({
        title: '录入成功...',
        icon: 'success',
        mask: true,
        duration: 1500,
        complete: function () {
          setTimeout(function () {
            wx.navigateBack();
          }, 1500)
        }
      });
    });
  },
  chooseCatogeryTap: function () {
    wx.navigateTo({
      url: "/pages/catogerySelect/catogerySelect"
    });
  }
})